package cn.aran.org.service.impl;

import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.basic.utils.MD5Utils;
import cn.aran.basic.utils.StrUtils;
import cn.aran.org.domain.Employee;
import cn.aran.org.mapper.EmployeeMapper;
import cn.aran.org.service.IEmployeeService;
import cn.aran.user.domain.Logininfo;
import cn.aran.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Description: 员工实现类
* @Author: Aran
* @Version: 1.0
* @Date:  2022/6/26 14:53
*/
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
    * @Title: initEmployee
    * @Description: 员工数据初始化
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 20:53
    * @Parameters: [employee]
    * @Return void
    */
    private void initEmployee(Employee employee){
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(saltPassword);
    }

    /**
    * @Title: employee2Logininfo
    * @Description: 将员工数据复制到logininfo表中
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 20:55
    * @Parameters: [employee]
    * @Return cn.aran.user.domain.Logininfo
    */
    private Logininfo employee2Logininfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    /**
    * @Title: initEmployeeRole
    * @Description: 员工关联表数据初始化
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/13 16:22
    * @Parameters: [employee]
    * @Return void
    */
    private void initEmployeeRole(Employee employee){
        employeeMapper.removeEmployeeRole(employee.getId());
        employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());
    }

    /**
    * @Title: save
    * @Description: 员工新增
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 20:36
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    public void save(Employee employee) {
        // 将employee参数初始化
        initEmployee(employee);
        // 将employee拷贝到logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 设置employee的logininfoId
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.save(employee);
        // 同步修改关联表
        initEmployeeRole(employee);
    }

    /**
    * @Title: remove
    * @Description: 员工删除
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 20:49
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void remove(Long id) {
        Long logininfoId = employeeMapper.loadById(id).getLogininfoId();
        logininfoMapper.remove(logininfoId);
    }

    /**
    * @Title: update
    * @Description: 员工修改
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 20:49
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    public void update(Employee employee) {
        // 将employee参数初始化
        initEmployee(employee);
        // 将employee拷贝到logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
        employeeMapper.update(employee);
        // 同步修改关联表
        initEmployeeRole(employee);
    }

    /**
    * @Title: loadByLogininfoId
    * @Description: 通过LogininfoId获取员工
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 22:51
    * @Parameters: [id]
    * @Return cn.aran.org.domain.Employee
    */
    @Override
    public Employee loadByLogininfoId(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    /**
    * @Title: loadByShopId
    * @Description: 查询同一商店下的员工
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/20 22:52
    * @Parameters: [shopId]
    * @Return java.util.List<cn.aran.org.domain.Employee>
    */
    @Override
    public List<Employee> loadByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}
